//
//  ST_MatchedListForChatCell.swift
//  btvideo
//
//  Created by mac on 2020/1/16.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import NIMSDK
import SwiftyUserDefaults

class ST_MatchedListForChatCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = Color.white
        
        contentView.addSubview(cover_image_view)
        contentView.addSubview(gradient_view)
        contentView.addSubview(time_label)
        contentView.addSubview(titleLabel)
        contentView.addSubview(content_label)
        contentView.addSubview(chat_btn)
        
        contentView.cornerRadius = 20
        
        cover_image_view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(cover_image_view.snp.width).dividedBy(165.0/160.0)
        }
        
        gradient_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(40)
            make.bottom.equalTo(cover_image_view)
        }
        
        time_label.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(cover_image_view).offset(-5)
        }
        
        
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cover_image_view.snp.bottom).offset(13)
            make.height.equalTo(17)
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10-30-5)
        }
        
        
        content_label.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
            make.left.equalTo(titleLabel)
            make.width.lessThanOrEqualTo((ScreenWidth-35)/2.0-40)
        }
        
        chat_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(30)
            make.centerY.equalTo(titleLabel.snp.bottom)
            make.right.equalToSuperview().offset(-10)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    MARK: - Properties
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.clipsToBounds = true
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        //        control.numberOfLines = 0
        //        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    fileprivate lazy var time_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.white
        //        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var gradient_view:ST_FWGradientView = {
        let control = ST_FWGradientView()
        
        control.bgLayer.colors = [UIColor(red: 0.12, green: 0.12, blue: 0.12, alpha: 0).cgColor, UIColor(red: 0, green: 0, blue: 0, alpha: 0.4).cgColor]
        control.bgLayer.locations = [0, 1]
        control.bgLayer.startPoint = CGPoint(x: 0, y: 0)
//        [UIColor(red: 0.12, green: 0.12, blue: 0.12, alpha: 0).cgColor, UIColor(red: 0.02, green: 0.02, blue: 0.02, alpha: 0.2).cgColor, UIColor(red: 0, green: 0, blue: 0, alpha: 0.4).cgColor]
        
        control.bgLayer.endPoint = CGPoint(x: 0, y: 1)
        return control
    }()
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#61666F")
        //        control.numberOfLines = 0
        //        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    fileprivate lazy var chat_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("消息_icon_发配对消息")!)
        control.add_touch_up(insideTarget: self, action: #selector(chat_handle))
        return control
    }()
    
    //    MARK:-
    weak var model:ST_BTSweetheart?{
        didSet{

            guard let m = model else{
                return
            }
            
            cover_image_view.pin_setImage(from: m.avatar.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            titleLabel.text = m.nickname
           
            time_label.text = m.createTime.b_t_global_date_string()
            
            content_label.text = m.age.string + "岁" + " \(m.adress)"
        }
    }
    
}

//MARK: - Event

extension ST_MatchedListForChatCell{
    
    @objc func chat_handle(){
        
        
        if BTVerticalAlertViewController.toastBindPhoneRequireAlert(fromController: self.parentViewController) {
            return
        }
        
        if self.parentViewController?.navigationController?.topViewController is ST_PrivateChatViewController{
                   
               print("连续点击-----")
               
               return
           }
        
        guard let m = model else { return  }
   
        
        push_to_chat_controller(info: ST_ChatInfoStruct.init(sessionId: m.accid, title: m.nickname, source: "聊天列表"), navigationController: self.parentViewController?.navigationController)
        
        /*
       let vc = ST_PrivateChatViewController()
       
       vc.sessionID = m.accid
       
       vc.chatTitle = m.nickname
        
        vc.source = "聊天列表"
                   
        self.parentViewController?.navigationController?.pushViewController(vc)
                   
       let session = NIMSession.init(vc.sessionID, type: NIMSessionType.P2P)
       
       NIMSDK.shared().conversationManager.markAllMessagesRead(in: session)
       
       ST_ConversationAdapter.shared.updateAllData()
        
         */
    }
}


//MARK: - derive Class
class ST_MyLoveCell: ST_MatchedListForChatCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.chat_btn.alpha = 0
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

struct ST_ChatInfoStruct {
    var sessionId:String
    var title:String
    var source:String?
    var intimacyImage:String?
}


func push_to_chat_controller(info:ST_ChatInfoStruct,navigationController:UINavigationController?)  {
    
    if BTVerticalAlertViewController.toastBindPhoneRequireAlert(fromController: navigationController) {
        return
    }
    
    let pushBlock = {
        
        let vc = ST_PrivateChatViewController()
        
        vc.sessionID = info.sessionId
        
        if let s = info.source{ vc.source = s }
                     
        if let nav = navigationController{
            nav.pushViewController(vc)
        }else{
            GlobalPresent(toController: vc)
        }
        
    }
    
    if ownPeculiarityCount < 1 {
        
        let alert = BTVerticalAlertViewController.createChatForPeculiarityAlert()
        
        alert.confirm_closure = {
            
            push_to_push_peculiarity_controller(navigationController: nil)
        }
        
        alert.cancel_closure = {
            pushBlock()
        }
        
        
        GlobalPresent(toController: alert)
        
    }else{
        
        pushBlock()
    }
    
    
    
    
    
    
    
}

